import Vue from 'vue'
// 引入埋点sdk
import { uuid } from '@/utils'
import * as api from '@/const/api'

export default function ({ $axios, app }) {
  const userAgent = navigator.userAgent
  const isMobile = /iPhone|iPad|iPod|Android/i.test(userAgent)
  const isTablet = /(iPad|Android)/i.test(userAgent) && !isMobile
  const isDesktop = !isMobile && !isTablet
  let one_id = app.$cookies.get('one_id')

  if (!one_id) {
    one_id = uuid()
    app.$cookies.set('one_id', one_id)
  }
  let device = ''

  if (isMobile) {
    device = 'mobile'
  } else if (isTablet) {
    device = 'tablet'
  } else if (isDesktop) {
    device = 'desktop'
  } else {
    device = 'other'
  }

  // 全局参数
  const options = {
    // 全渠道唯一id 后期通过接口换取 现阶段使用memberId
    oneId: one_id,
    // 账号类型(visitor/member) 游客身份可不传one_id
    device, // 终端设备
    browser: navigator.userAgent, // 浏览器
    os: navigator.platform, // 操作系统,
    fromSource: '',
  }

  if (!Vue.$track) {
    // // 注入到vue原型 通过 this.$track.track 调用埋点
    Vue.prototype.$track = function (params) {
      $axios.$post(api.dmTrackUrl, {
        ...options,
        ...params,
      })
    }
  }
}
